package com.gurun.codes.business.home;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gurun.base.DebounceObservableTransformer;
import com.gurun.base.Logger;
import com.gurun.codes.R;
import com.gurun.codes.databinding.ItemHomeRoomListBinding;
import com.gurun.codes.utils.ScreenUtils;
import com.gurun.model.bean.home.GetRoomListResultBean;
import com.jakewharton.rxbinding2.view.RxView;

import io.reactivex.functions.Consumer;

class RoomListtemViewBinder extends me.drakeet.multitype.ItemViewBinder<GetRoomListResultBean.SmartRoomListBean, RoomListtemViewBinder.ViewHolder> {

    private Context mContext;
    private MainActivityContract.MainActivityView mMainActivityView;

    public RoomListtemViewBinder(Context context, MainActivityContract.MainActivityView mainActivityView) {
        mContext = context;
        mMainActivityView = mainActivityView;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_home_room_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull GetRoomListResultBean.SmartRoomListBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private GetRoomListResultBean.SmartRoomListBean mData;
        private ItemHomeRoomListBinding mBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            mBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(GetRoomListResultBean.SmartRoomListBean item) {
            mData = item;
            if (mData != null) {
                mBinding.setData(mData);
                mBinding.tvName.setText(mData.getRoomName());
                RxView.clicks(mBinding.llItem)
                        .compose(new DebounceObservableTransformer<>())
                        .subscribe(new Consumer<Object>() {

                            @Override
                            public void accept(Object o) throws Exception {
                                if (mMainActivityView != null) {
                                    mMainActivityView.onRoomItemSelected(mData);
                                }
                            }
                        });
                mBinding.llItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View view, boolean b) {
                       ViewGroup.LayoutParams layoutParams = mBinding.llItem.getLayoutParams();
                        if (b) {
                            mBinding.tvName.setTextSize(ScreenUtils.getXmlDef(mContext, R.dimen.base_text_size_l));
                            mBinding.tvName.setTextColor(mContext.getResources().getColor(R.color.home_item_select_text_color));
                            mBinding.ivIcon.setImageResource(R.mipmap.ic_home_room_sel);
                            layoutParams.height = ScreenUtils.dp2px(mContext,100);
                        } else {
                            mBinding.tvName.setTextSize(ScreenUtils.getXmlDef(mContext, R.dimen.base_text_size));
                            mBinding.tvName.setTextColor(mContext.getResources().getColor(R.color.white));
                            mBinding.ivIcon.setImageResource(R.mipmap.ic_home_room);
                            layoutParams.height = ScreenUtils.dp2px(mContext,80);
                        }
                        mBinding.llItem.setLayoutParams(layoutParams);
                    }
                });
            }
        }
    }
}
